home *** CD-ROM | disk | FTP | other *** search
/ Graphics & Sound Program…ng Techniques for the Mac / Graphics and Sound Programming Techniques for the Mac.iso / M&T Graphics & Sound Examples / Symantec Versions / C04 Speech / P06 Change Voice / ChangeVoice.c next >
Encoding:
C/C++ Source or Header  |  1995-08-31  |  6.3 KB  |  229 lines  |  [TEXT/KAHL]

  1. //____________________________________________________________
  2. //    ChangeVoice.c
  3. //
  4. //    Copyright © Dan Parks Sydow, 1995
  5. //    From the book:
  6. //    "Graphics and Sound Programming Techniques for the Mac",
  7. //    M&T Books, 1995
  8.  
  9.  
  10. //____________________________________________________________
  11.  
  12. #include <Speech.h>
  13.  
  14.  
  15. //____________________________________________________________
  16.  
  17. void           InitializeToolbox( void );
  18. Boolean        IsSpeechAvailable( void );
  19. SpeechChannel  OpenOneSpeechChannelVoice( VoiceSpec );
  20. void           OpenSpeechDialog( void );
  21. OSErr          GetVoiceSpecBasedOnAgeGender( VoiceSpec *, short, short, short );
  22.  
  23.  
  24. //____________________________________________________________
  25.  
  26. #define         rSpeechDialog                     128
  27. #define         kPlaySpeechButton                   1
  28. #define         kSetSpeechYoungBoyButton            2
  29. #define         kSetSpeechMiddleAgeWomanButton      3
  30. #define         kSetSpeechRobotButton               4
  31. #define         kSetSpeechDefaultButton             5
  32. #define         kQuitButton                         6
  33. #define         kNoMatchingVoiceErr              -999
  34.  
  35. //____________________________________________________________
  36.  
  37. void  main( void )
  38.    Boolean  speechPresent;
  39.    
  40.    InitializeToolbox();
  41.  
  42.    speechPresent = IsSpeechAvailable();
  43.    if ( speechPresent == false )
  44.       ExitToShell();
  45.  
  46.    OpenSpeechDialog();
  47. }
  48.  
  49.  
  50. //____________________________________________________________
  51.  
  52. void  OpenSpeechDialog( void )
  53. {
  54.    DialogPtr         theDialog;
  55.    short             theItem;
  56.    Boolean           allDone = false;
  57.    OSErr             theError;
  58.    SpeechChannel     theChannel;
  59.    Str255            theString = "\pMilwaukee, that's in Minnesota isn't it?";
  60.    short             theAgeLo;
  61.    short             theAgeHi;
  62.    short             theGender;
  63.    VoiceSpec         theDefaultVoiceSpec;
  64.    VoiceSpec         theVoiceSpec;
  65.    VoiceDescription  theVoiceDesc;
  66.    
  67.    theDialog = GetNewDialog( rSpeechDialog, nil, (WindowPtr)-1L );
  68.    ShowWindow( theDialog );
  69.    SetPort( theDialog );
  70.    
  71.    theError = GetVoiceDescription( nil, &theVoiceDesc, sizeof( theVoiceDesc ) ); 
  72.    
  73.    theDefaultVoiceSpec = theVoiceDesc.voice;
  74.    theVoiceSpec = theDefaultVoiceSpec;
  75.    
  76.    while ( allDone == false )
  77.    {
  78.       ModalDialog( nil, &theItem );
  79.              
  80.       switch ( theItem )
  81.       {
  82.          case kSetSpeechYoungBoyButton:
  83.             theGender = kMale;
  84.             theAgeLo  =  5;
  85.             theAgeHi  = 10;
  86.             theError = GetVoiceSpecBasedOnAgeGender( &theVoiceSpec, theAgeLo, 
  87.                                                      theAgeHi, theGender );
  88.             break;
  89.  
  90.          case kSetSpeechMiddleAgeWomanButton:
  91.             theGender = kFemale;
  92.             theAgeLo  = 30;
  93.             theAgeHi  = 50;
  94.             theError = GetVoiceSpecBasedOnAgeGender( &theVoiceSpec, theAgeLo, 
  95.                                                      theAgeHi, theGender );
  96.             break;
  97.  
  98.          case kSetSpeechRobotButton:
  99.             theGender = kNeuter;
  100.             theAgeLo  =   500;
  101.             theAgeHi  = 10000;
  102.             theError = GetVoiceSpecBasedOnAgeGender( &theVoiceSpec, theAgeLo, 
  103.                                                      theAgeHi, theGender );
  104.             break;
  105.  
  106.          case kSetSpeechDefaultButton:
  107.             theVoiceSpec = theDefaultVoiceSpec;
  108.             break;
  109.             
  110.          case kPlaySpeechButton:
  111.             theChannel = OpenOneSpeechChannelVoice( theVoiceSpec );
  112.             if ( theChannel == nil )
  113.                ExitToShell();               
  114.             theError = SpeakText( theChannel, (Ptr)(theString + 1), theString[0] );
  115.             while ( SpeechBusy() == true )
  116.                ;                    
  117.             theError = DisposeSpeechChannel( theChannel );
  118.             if ( theError != noErr )
  119.                ExitToShell();  
  120.             break;
  121.  
  122.          case kQuitButton:
  123.             allDone = true;
  124.             break;
  125.       }
  126.       
  127.       if ( theError == kNoMatchingVoiceErr )
  128.       {
  129.          SysBeep( 0 );
  130.          theVoiceSpec = theDefaultVoiceSpec;
  131.       }
  132.       else if ( theError != noErr )
  133.          ExitToShell();
  134.    }
  135.  
  136.    DisposeDialog( theDialog ); 
  137. }
  138.  
  139. //____________________________________________________________
  140.  
  141. OSErr  GetVoiceSpecBasedOnAgeGender( VoiceSpec *theVoiceSpec, 
  142.                                      short      theAgeLo, 
  143.                                      short      theAgeHi, 
  144.                                      short      theGender )
  145. {
  146.    OSErr             theError;
  147.    short             theNumVoices;
  148.    short             theIndex;
  149.    VoiceDescription  theVoiceDesc;
  150.    
  151.    theError = CountVoices( &theNumVoices );
  152.    if ( theError != noErr )
  153.       return ( theError );
  154.  
  155.    for ( theIndex = 1; theIndex <= theNumVoices; theIndex++ )
  156.    {
  157.       theError = GetIndVoice( theIndex, theVoiceSpec );
  158.       if ( theError != noErr )
  159.          return ( theError );
  160.  
  161.       theError = GetVoiceDescription( theVoiceSpec, &theVoiceDesc, 
  162.                                       sizeof( theVoiceDesc ) );      
  163.       if ( theError != noErr )
  164.          return ( theError );
  165.       
  166.       if ( (theVoiceDesc.age >= theAgeLo) && (theVoiceDesc.age <= theAgeHi) )
  167.          if ( theVoiceDesc.gender == theGender )
  168.             return ( noErr );
  169.    }
  170.    return ( kNoMatchingVoiceErr );
  171. }
  172.  
  173.  
  174. //____________________________________________________________
  175.  
  176. SpeechChannel  OpenOneSpeechChannelVoice( VoiceSpec theVoiceSpec )
  177. {
  178.    SpeechChannel  theChannel;   
  179.    OSErr          theError;
  180.    
  181.    theError = NewSpeechChannel( &theVoiceSpec, &theChannel );
  182.    if ( theError != noErr )
  183.    {
  184.       theError = DisposeSpeechChannel( theChannel );
  185.       theChannel = nil;
  186.    }
  187.       
  188.    return ( theChannel );
  189. }
  190.  
  191.  
  192. //____________________________________________________________
  193.  
  194. Boolean  IsSpeechAvailable( void )
  195. {
  196.    OSErr    theError;
  197.    long     theResult;
  198.    Boolean  speechAvail;
  199.    
  200.    theError = Gestalt( gestaltSpeechAttr, &theResult );
  201.    if ( theError != noErr )
  202.       ExitToShell();
  203.       
  204.    speechAvail = theResult & ( 1 << gestaltSpeechMgrPresent );  
  205.    if ( speechAvail > 0 )
  206.       return ( true );
  207.    else
  208.       return ( false );
  209. }
  210.  
  211.  
  212. //____________________________________________________________
  213.  
  214. void  InitializeToolbox( void )
  215. {
  216.    InitGraf( &qd.thePort );
  217.    InitFonts();
  218.    InitWindows();
  219.    InitMenus();
  220.    TEInit();
  221.    InitDialogs( 0L );
  222.    FlushEvents( everyEvent, 0 );
  223.    InitCursor();
  224. }
  225.  
  226.  
  227.  
  228.